package com.putable.nmc.world;

import java.util.EnumMap;
import java.util.EnumSet;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.Scanner;

public class CustomerDistribution {
    double timeStarts;
    double timeEnds;
    double sumArrivalTimes;
    Map<CustomerType,Double> arrivalDistribution = 
        new EnumMap<CustomerType,Double>(CustomerType.class);

    public String toString() {
        StringBuffer ret = new StringBuffer();
        ret.append(""+timeStarts);
        ret.append(" "+timeEnds);
        for (CustomerType c : arrivalDistribution.keySet()) {
            ret.append(" "+arrivalDistribution.get(c));
        }
        return ret.toString();
    }

    public static CustomerDistribution fromString(String s) 
                                throws InputMismatchException {
        Scanner sc = new Scanner(s);
        CustomerDistribution ret = new CustomerDistribution();
        ret.timeStarts = sc.nextDouble();
        if (ret.timeStarts < 0)
            throw new InputMismatchException("Bad start time");
        ret.timeEnds = sc.nextDouble();
        if (ret.timeEnds < ret.timeStarts)
            throw new InputMismatchException("Bad endtime");
        ret.sumArrivalTimes = 0.0;
        for (CustomerType c : EnumSet.allOf(CustomerType.class)) {
            double avgRate = sc.nextDouble();
            if (avgRate < 0)
                throw new InputMismatchException("Bad rate");
            ret.sumArrivalTimes += avgRate;
            ret.arrivalDistribution.put(c,avgRate);
        }
        return ret;
    }
}
